package com.zhouzhou.fastjava.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;

import java.io.UnsupportedEncodingException;

public class ResponseWrapper {

    private final HttpRequest httpRequest;

    private final MyHttpResponse httpResponse;

    private final MyHttpRequest myHttpRequest;

    public ResponseWrapper(HttpRequest httpRequest, MyHttpResponse httpResponse, MyHttpRequest myHttpRequest) {
        this.httpRequest = httpRequest;
        this.httpResponse = httpResponse;
        this.myHttpRequest = myHttpRequest;
    }

    public String asText() {
        try {
            return new String(httpResponse.getResBytes(), myHttpRequest.getCharset());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public JSONObject asJson() {
        return JSON.parseObject(asText());
    }

    public byte[] asBytes() {
        return httpResponse.getResBytes();
    }

    public HttpResponse asHttpResponse() {
        return httpResponse;
    }

    public int statusCode() {
        return httpResponse.getStatusLine().getStatusCode();
    }

}
